package com.zryc.shop.sms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.sms.entity.HomeBrand;
import com.zryc.shop.sms.entity.HomeNewProduct;
import com.zryc.shop.sms.mapper.HomeNewProductMapper;
import com.zryc.shop.sms.service.IHomeNewProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 新鲜好物表 服务实现类
 * </p>
 *
 * @author zhaozhao
 * @since 2024-01-18
 */
@Service
public class HomeNewProductServiceImpl extends ServiceImpl<HomeNewProductMapper, HomeNewProduct> implements IHomeNewProductService {

    /**
     * 分页查询
     *
     * @param newProductName
     * @param recommendStatus
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<HomeNewProduct> list(String newProductName, Integer recommendStatus, Integer pageNum, Integer pageSize) {
        Page<HomeNewProduct> page = new Page<>(pageNum, pageSize);
        QueryWrapper<HomeNewProduct> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(newProductName)) {
            wrapper.like("product_name", newProductName);
        }
        if (recommendStatus != null) {
            wrapper.eq("recommend_status",recommendStatus);
        }
        return page(page, wrapper);
    }

    /**
     * 设置推荐排序值
     *
     * @param id
     * @param sort
     * @return
     */
    @Override
    public Object updateSort(Long id, Integer sort) {
        UpdateWrapper<HomeNewProduct> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("sort", sort);
        update(updateWrapper);
        return null;
    }

    /**
     * 设置推荐/取消
     *
     * @param ids
     * @param recommendStatus
     * @return
     */
    @Override
    public Object updateRecommendStatus(String ids, Integer recommendStatus) {
        String[] idList=ids.split(",");
        UpdateWrapper<HomeNewProduct> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", idList);
        updateWrapper.set("recommend_status", recommendStatus);
        update(updateWrapper);
        return null;
    }
}
